<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Like
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: LikeButton.php 2010-09-07 16:05 idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Like
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Like_View_Helper_LikeButton extends Engine_View_Helper_HtmlImage
{
  public function likeButton($item, $attrs = array(), $requireUser = true, $viewer_liked = null)
  {
    if( !($item instanceof Core_Model_Item_Abstract)){
      throw new Zend_View_Exception("Item must be a valid item");
    }

    if ($requireUser && !Engine_Api::_()->user()->getViewer()->getIdentity()){
      return "";
    }

    if ($viewer_liked === null){
      $viewer_liked = Engine_Api::_()->like()->isLike($item);
    }

    if (!$viewer_liked){
      $label = $this->view->translate('like_Like');
      $class = 'like_button';
      $action = 'like';
      $onClick = (!empty($attrs['onLike'])) ? ' onClick = "' . $attrs['onLike'] . '"' : '';
    }
    else{
      $label = $this->view->translate('like_Unlike');
      $class = 'unlike_button';
      $action = 'unlike';
      $onClick = (!empty($attrs['onUnlike'])) ? ' onClick = "' . $attrs['onUnlike'] . '"' : '';
    }

    $attributes = "";
    if (!empty($attrs)){
      unset($attrs['class']);
      unset($attrs['id']);
      unset($attrs['href']);
      unset($attrs['onLike']);
      unset($attrs['onUnlike']);

      foreach ($attrs as $key => $value){
        $attributes .= " " .$key . "='" . $value . "'";
      }
    }

    $style = '';
    if (!Engine_Api::_()->like()->isEnabledLikeClub($item)){
      $style = "style='display:none;'";
    }

    $wrapper = ' '
      . '<div class="like_button_container" ' . $style . ' >'
      . '<a ' . $attributes . '  id="_' . $item->getGuid() . '" '. $onClick .' class="like_button_link ' . $action . '" href="javascript:void(0)">'
      . '<span class="' . $class . '">' . $label . '</span>'
      . '</a>'
      . '</div>';

    return $wrapper;
  }
}